﻿// 版权所有：
// 文 件  名：FileDialogHelper.cs
// 功能描述：
// 创建标识：Seven Song(m.sh.lin0328@163.com) 2014/3/18 21:26:03
// 修改描述：
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MSL.Tool
{
    /// <summary>
    /// 打开、保存文件
    /// </summary>
    public class Dialog
    {
        public static String ExcelFilter = "Excel(*.xls;*.xlsx)|*.xls;*.xlsx|All File(*.*)|*.*";
        public static String WordFilter = "Word(*.doc;*.docx;*.wps)|*.doc;*.docx;*.wps|All File(*.*)|*.*";
        public static String ImageFilter = "Image Files(*.BMP;*.bmp;*.JPG;*.jpg;*.GIF;*.gif;*.png)|(*.BMP;*.bmp;*.JPG;*.jpg;*.GIF;*.gif;*.png)|All File(*.*)|*.*";
        public static String HtmlFilter = "HTML files (*.html;*.htm)|*.html;*.htm|All files (*.*)|*.*";
        public static String AccessFilter = "Access(*.mdb)|*.mdb|All File(*.*)|*.*";
        public static String ZipFillter = "Zip(*.zip)|*.zip|All files (*.*)|*.*";
        public static String ConfigFilter = "配置文件(*.ini;*.xml;*.config)|*.ini;*.xml;*.config|All File(*.*)|*.*";
        public static String TxtFilter = "(*.txt)|*.txt|All files (*.*)|*.*";

        /// <summary>
        /// 打开文件夹
        /// </summary>
        /// <returns></returns>
        public static string OpenDir()
        {
            return OpenDir(String.Empty);
        }

        /// <summary>
        /// 打开文件夹
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string OpenDir(string path)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择路径";
            dialog.SelectedPath = path;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                return dialog.SelectedPath;
            }
            else
            {
                return String.Empty;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="title"></param>
        /// <param name="filter"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static string Open(string title, string filter, string filename)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = filter;
            dialog.Title = title;
            dialog.RestoreDirectory = true;
            dialog.FileName = filename;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                return dialog.FileName;
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="title"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public static string Open(string title, string filter)
        {
            return Open(title, filter, string.Empty);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="title"></param>
        /// <param name="filter"></param>
        /// <param name="filename"></param>
        /// <param name="initialDirectory"></param>
        /// <returns></returns>
        public static string Save(string title, string filter, string filename, string initialDirectory)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Filter = filter;
            dialog.Title = title;
            dialog.FileName = filename;
            dialog.RestoreDirectory = true;
            if (!String.IsNullOrEmpty(initialDirectory))
            {
                dialog.InitialDirectory = initialDirectory;
            }
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                return dialog.FileName;
            }
            return string.Empty;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="title"></param>
        /// <param name="filter"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static string Save(string title, string filter, string filename)
        {
            return Save(title, filter, filename, "");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="title"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public static string Save(string title, string filter)
        {
            return Save(title, filter, string.Empty);
        }

        /// <summary>
        /// 获取颜色对话框的颜色
        /// </summary>
        /// <returns></returns>
        public static Color PickColor()
        {
            Color result = SystemColors.Control;
            ColorDialog dialog = new ColorDialog();
            if (DialogResult.OK == dialog.ShowDialog())
            {
                result = dialog.Color;
            }
            return result;
        }

        /// <summary>
        /// 获取颜色对话框的颜色
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        public static Color PickColor(Color color)
        {
            Color result = SystemColors.Control;
            ColorDialog dialog = new ColorDialog();
            if (color != null)
            {
                dialog.Color = color;
            }
            if (DialogResult.OK == dialog.ShowDialog())
            {
                result = dialog.Color;
            }
            return result;
        }
    }
}
